package org.javaweb.cms_web.mapper;

import org.apache.ibatis.annotations.*;
import org.javaweb.cms_web.model.ArticleDTO;

import java.util.List;

@Mapper
public interface ArticleManagementMapper {



    // 根据团队ID查询所有文章
    @Select("SELECT * FROM articles WHERE team_id = #{teamID} AND status = 1 ORDER BY publish_date DESC")
    @Results({
            @Result(property = "articleId", column = "article_id"),
            @Result(property = "title", column = "title"),
            @Result(property = "abstractText", column = "abstract"),
            @Result(property = "content", column = "content"),
            @Result(property = "attachmentLink", column = "attachment_link"),
            @Result(property = "publishDate", column = "publish_date"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "teamId", column = "team_id"),
            @Result(property = "status", column = "status")
    })
    List<ArticleDTO> findArticlesByTeam(@Param("teamID") int teamID);

    // 根据团队ID和文章标题模糊查询文章
    @Select("SELECT * FROM articles WHERE team_id = #{teamID} AND title LIKE #{title} AND status = 1 ORDER BY publish_date DESC")
    @Results({
            @Result(property = "articleId", column = "article_id"),
            @Result(property = "title", column = "title"),
            @Result(property = "abstractText", column = "abstract"),
            @Result(property = "content", column = "content"),
            @Result(property = "attachmentLink", column = "attachment_link"),
            @Result(property = "publishDate", column = "publish_date"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "teamId", column = "team_id"),
            @Result(property = "status", column = "status")
    })
    List<ArticleDTO> findArticlesByTeamAndTitle(@Param("teamID") int teamID, @Param("title") String title);

    // 获取所有文章
    @Select("SELECT article_id, title, abstract, content, attachment_link, publish_date, user_id, team_id, status FROM Articles")
    @Results({
            @Result(property = "articleId", column = "article_id"),
            @Result(property = "title", column = "title"),
            @Result(property = "abstractText", column = "abstract"),
            @Result(property = "content", column = "content"),
            @Result(property = "attachmentLink", column = "attachment_link"),
            @Result(property = "publishDate", column = "publish_date"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "teamId", column = "team_id"),
            @Result(property = "status", column = "status")
    })
    List<ArticleDTO> findAllArticles();

    // 插入新文章
    @Insert("INSERT INTO Articles (title, abstract, content, attachment_link, publish_date, user_id, team_id, status) " +
            "VALUES (#{title}, #{abstractText}, #{content}, #{attachmentLink}, #{publishDate}, #{userId}, #{teamId}, 0)")
    @Options(useGeneratedKeys = true, keyProperty = "articleId")
    int insertArticle(ArticleDTO article);

    // 根据文章ID查询文章
    @Select("SELECT * FROM Articles WHERE article_id = #{articleId}")
    @Results({
            @Result(property = "articleId", column = "article_id"),
            @Result(property = "title", column = "title"),
            @Result(property = "abstractText", column = "abstract"),
            @Result(property = "content", column = "content"),
            @Result(property = "attachmentLink", column = "attachment_link"),
            @Result(property = "publishDate", column = "publish_date"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "teamId", column = "team_id"),
            @Result(property = "status", column = "status")
    })
    ArticleDTO findArticleById(Integer articleId);

    // 更新文章
    @Update("UPDATE Articles SET title = #{title}, abstract = #{abstractText}, content = #{content}, attachment_link = #{attachmentLink} WHERE article_id = #{articleId}")
    int updateArticle(ArticleDTO article);

    // 删除文章
    @Delete("DELETE FROM Articles WHERE article_id = #{articleId}")
    int deleteArticle(Integer articleId);

    // 根据条件查询文章
    @Select("<script>" +
            "SELECT article_id, title, abstract, content, attachment_link, publish_date, user_id, team_id, status " +
            "FROM Articles " +
            "WHERE 1=1 " +
            "<if test='searchKeyword != null and searchKeyword != \"\"'> AND title LIKE CONCAT('%', #{searchKeyword}, '%') </if> " +
            "<if test='startDate != null and startDate != \"\"'> AND publish_date &gt;= #{startDate} </if> " +
            "<if test='endDate != null and endDate != \"\"'> AND publish_date &lt;= #{endDate} </if> " +
            "</script>")
    @Results({
            @Result(property = "articleId", column = "article_id"),
            @Result(property = "title", column = "title"),
            @Result(property = "abstractText", column = "abstract"),
            @Result(property = "content", column = "content"),
            @Result(property = "attachmentLink", column = "attachment_link"),
            @Result(property = "publishDate", column = "publish_date"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "teamId", column = "team_id"),
            @Result(property = "status", column = "status")
    })
    List<ArticleDTO> findArticlesByConditions(@Param("searchKeyword") String searchKeyword,
                                              @Param("startDate") String startDate,
                                              @Param("endDate") String endDate);

    //根据审核状态查询文章
    @Select("SELECT article_id, title, abstract, content, attachment_link, publish_date, user_id, team_id, status FROM Articles WHERE status = #{status}")
    @Results({
            @Result(property = "articleId", column = "article_id"),
            @Result(property = "title", column = "title"),
            @Result(property = "abstractText", column = "abstract"),
            @Result(property = "content", column = "content"),
            @Result(property = "attachmentLink", column = "attachment_link"),
            @Result(property = "publishDate", column = "publish_date"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "teamId", column = "team_id"),
            @Result(property = "status", column = "status")
    })
    List<ArticleDTO> findArticlesByStatus(Integer status);

    // 更新文章状态
    @Update("UPDATE Articles SET status = #{status} WHERE article_id = #{articleId}")
    int updateArticleStatus(@Param("articleId") Integer articleId, @Param("status") Integer status);

    // 更新文章驳回原因
    @Update("UPDATE Articles SET reject_reason = #{rejectReason} WHERE article_id = #{articleId}")
    int updateRejectReason(@Param("articleId") Integer articleId, @Param("rejectReason") String rejectReason);

    @Update("UPDATE Articles SET status = #{status} WHERE article_id = #{articleId}")
    int updateStatus(@Param("articleId") Integer articleId, @Param("status") Integer status);
}
